package ms.platform.dict.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import ms.platform.dict.model.DictModel;
import ms.platform.dict.service.DictService;
import ms.platform.system.controller.BaseController;
import ms.platform.util.PageUtils;

import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/dict/")
public class DictController extends BaseController{

	@Autowired
	private DictService dictService;
	
	/**
	 * 字典列表
	 * @param map
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(value = "/list")
	public String list(@RequestParam Map<String,Object> map, HttpServletRequest request) throws UnsupportedEncodingException{
		map.put("level", "0");
		map.put("count", dictService.queryDictCount(map));
		List<DictModel> list = dictService.queryDictListByCondition(PageUtils.Page(request,map));
		request.setAttribute("list", list);
		PageUtils.calculate(map);
		return "dict/list";
	}
	
	/**
	 * 数据列表
	 * @param map
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(value = "/listData")
	public String listData(@RequestParam Map<String,Object> map, HttpServletRequest request) throws UnsupportedEncodingException{
		map.put("level", "1");
		map.put("count", dictService.queryDictCount(map));
		List<DictModel> list = dictService.queryDictListByCondition(PageUtils.Page(request,map));
		request.setAttribute("list", list);
		PageUtils.calculate(map);
		request.setAttribute("parentId", map.get("parentId"));
		return "dict/listData";
	}

	/**
	 * 删除字典数据
	 * @param dictId
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/deleteDict")
	@ResponseBody
	public Object deleteDict(Integer id, HttpServletRequest request){
		dictService.deleteById(id);
		return returnSuccess("操作成功",id);
	}

	/**
	 * 跳转字典分组添加页面
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/toAddDict")
	public String toAddDict(HttpServletRequest request){
		return "dict/add";
	}
	/**
	 * 跳转字典数据添加页面
	 * @param id
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(value = "/toAddDictData")
	public String toAddDictData( HttpServletRequest request,String parentId) throws UnsupportedEncodingException{
		request.setAttribute("parentId", parentId);
		return "dict/addData";
	}
	
	/**
	 * 添加字典项
	 * @param dict
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/save")
	@ResponseBody
	public Object save(DictModel dict, HttpServletRequest request){
		String adminId = (String) request.getSession().getAttribute("admin");
		dict.setCreateBy(adminId);
		dict.setLevel("0");
		dictService.insert(dict);
		return returnSuccess("操作成功", dict);
	}
	
	/**
	 * 添加数据项
	 * @param dict
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/saveData")
	@ResponseBody
	public Object saveData(DictModel dict, HttpServletRequest request){
		String adminId = (String) request.getSession().getAttribute("admin");
		Map <String,Object> map=new HashMap<String,Object>();
		dict.setCreateBy(adminId);
		dict.setLevel("1");
		map.put("dictCode", dict.getDictCode());
		map.put("parentId", dict.getParentId());
		List<DictModel> dictModel = dictService.queryGroupCode(map);
		if(dictModel.size()>0){
			return returnError(1,"该分组编码存在，请重新填写",dict);
		}
		dictService.insert(dict);
		request.setAttribute("parentId", dict.getParentId());
		return returnSuccess("操作成功", dict);
	}
	
	/**
	 * 跳转字典编辑页面
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/toEdit")
	public String toEdit(Integer id, HttpServletRequest request){
		if(id!= null){
			DictModel dict = dictService.queryDictById(id);
			request.setAttribute("dict", dict);
		}
		return "dict/edit";
	}
	
	/**
	 * 跳转数据编辑页面
	 * @param id
	 * @param groupCode
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(value = "/toEditData")
	public String toEditData(Integer id, String groupCode,HttpServletRequest request) throws UnsupportedEncodingException{
		if(id!= null){
			DictModel dict = dictService.queryDictById(id);
			request.setAttribute("dict", dict);
		}
		return "dict/editData";
	}
	
	/**
	 * 改状态
	 * @param id
	 * @param groupCode
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(value = "/statusData")
	@ResponseBody
	public Object savesData(Integer id,String status, HttpServletRequest request){
		DictModel dict = new DictModel();
		dict.setId(id);
		dict.setStatus(status);
		dictService.updateStatus(dict);
		return returnSuccess("操作成功", id);
	}

	/**
	 * 修改字典
	 * @param dict
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/update")
	@ResponseBody
	public Object update(DictModel dict, HttpServletRequest request){
		if(dict.getId() != null){
			String adminId = (String) request.getSession().getAttribute("admin");
			dict.setUpdateBy(adminId);
			dictService.update(dict);
		}
		return returnSuccess("操作成功", dict);
	}
	
	/**
	 * 检查分组编码是否已经存在
	 * @param group_code
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getGroupCodeByName")
	@ResponseBody
	public Object getGroupCodeByName(String dictCode, HttpServletRequest request){
		boolean result = true;
		DictModel dict = dictService.queryGroupCodeByName(dictCode.trim());
		if(dict != null){
			result = false;
		}
		Map<String, Boolean> map = new HashMap<>();
        map.put("valid", result);
        ObjectMapper mapper = new ObjectMapper();
        	String resultString = "";
	        try {
	            resultString = mapper.writeValueAsString(map);
	        } catch (JsonProcessingException e) {
	            e.printStackTrace();
	        } catch (IOException e) {
				e.printStackTrace();
			}
	        return resultString;
	}
}	
